<template>
  <div class="qingwu">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <!-- <div><el-input v-model="name" placeholder="请输入内容"></el-input></div>
                    <div><el-button icon="el-icon-search">条件筛选</el-button></div> -->
          <div>
            <el-input class="input" v-model="where.name" placeholder="请输入关键词名称"></el-input>
          </div>
          <div style="width:160px;">
            <el-select v-model="where.type" placeholder="*选择关联类型">
              <el-option label="商品" :value="1"></el-option>
              <el-option label="项目" :value="2"></el-option>
            </el-select>
          </div>
          <div>
            <el-button icon="el-icon-search" @click="get_keyword_list()">条件筛选</el-button>
          </div>
          <div>
            <router-link to="/Admin/keyword/keyword_add">
              <el-button icon="el-icon-plus">添加关键词</el-button>
            </router-link>
          </div>
        </div>
        <!-- <div class="admin_main_block_right">
                    <div><el-button type="danger" icon="el-icon-delete" @click="del(select_id)">批量删除</el-button></div>
                </div> -->
      </div>
      <div class="admin_table_main">
        <el-table :data="list" @selection-change="handleSelectionChange">
          <!-- <el-table-column type="selection"></el-table-column> -->
          <el-table-column label="序号" width="50px">
            <template slot-scope="scope">
              <div>{{scope.$index+1}}</div>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="关键词名称" width="150px"></el-table-column>
          <el-table-column prop="desc" label="关键词描述" width="200px"></el-table-column>
          <el-table-column label="关联类型">
            <template slot-scope="scope">
              <div>{{scope.row.type==1?'商品':'项目'}}</div>
            </template>
          </el-table-column>
          <el-table-column prop="relation_count" label="关联数"></el-table-column>
          <el-table-column label="APP首页推荐" width="95px">
            <template slot-scope="scope">
              <div v-if="scope.row.type==2" class="status_center" :class="scope.row.index_push==1?'green_round':'gray_round'"
                @click="exchange_status(scope.row.id,scope.row.type,scope.row.index_push)"></div>
            </template>
          </el-table-column>
          <el-table-column label="商城首页推荐" width="95px">
            <template slot-scope="scope">
              <div v-if="scope.row.type==1" class="status_center" :class="scope.row.mall_push==1?'green_round':'gray_round'"
                @click="exchange_status(scope.row.id,scope.row.type,scope.row.mall_push)"></div>
            </template>
          </el-table-column>
          <el-table-column prop="sort" label="首页排序"></el-table-column>
          <el-table-column label="创建时间" width="115px">
            <template slot-scope="scope">
              <div>{{scope.row.created_at|formatDate}}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="150px">
            <template slot-scope="scope">
              <el-button type="primary" size="mini"
                @click="$router.push({name:'admin_keyword_add',params:{id:scope.row.id}})">编辑</el-button>
              <el-button type="danger" size="mini" @click="del(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination @current-change="current_change" background layout="prev, pager, next,jumper,total" :total="total_data"
            :page-size="page_size" :current-page="current_page"></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      list: [],
      total_data: 0, // 总条数
      page_size: 20,
      current_page: 1,
      select_id: '',
      where: {
        name: '',
        type: '',
      },
    };
  },
  watch: {},
  computed: {},
  methods: {
    handleSelectionChange: function (e) {
      let ids = [];
      e.forEach(v => {
        ids.push(v.id);
      });
      this.select_id = ids.join(',');
    },
    get_keyword_list: function () {
      this.where.page = this.current_page;
      if (!this.where.type) delete this.where.type;
      if (!this.where.name) delete this.where.name;
      this.$get(this.$api.getKeywordList, this.where).then(res => {
        this.page_size = res.data.per_page;
        this.total_data = res.data.total;
        this.current_page = res.data.current_page;
        this.list = this.$formatGoods(res.data.data);
      })
    },
    // 删除处理
    del: function (keyword_id) {
      if (this.$isEmpty(keyword_id)) {
        return this.$message.error('请先选择删除的对象');
      }
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$post(this.$api.keywordDel, { keyword_id }).then(res => {
          if (res.code == 200) {
            this.get_keyword_list();
            return this.$message.success("删除成功");
          } else {
            return this.$message.error("删除失败");
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    // 指定ID修改状态
    exchange_status: function (keyword_id, type, status) {
      type = type == 1 ? 'mall_push' : 'index_push';
      status = status == 0 ? 1 : 0;
      this.$post(this.$api.keywordSwitch, { keyword_id, type, status }).then(res => {
        if (res.code == 200) {
          this.$message.success('操作成功');
        } else {
          this.$message.success('操作失败');
        }
        this.get_keyword_list();
      });
    },
    current_change: function (e) {
      this.current_page = e;
      this.get_keyword_list();
    },
  },
  created() {
    this.get_keyword_list();
  },
  mounted() { }
};
</script>
<style lang="scss" scoped>
</style>